<?php 
require_once "cw2conf.php";
include_once "misc.php";
?>

<html>
<head><title>Clan War Map</title>

<?php
echo '
<script type="text/javascript">
var CW2_NO_OBJECT = '.CW2_NO_OBJECT.';
var CW2_NO_SID = '.CW2_NO_SID.';
</script>';
?>

<script type="text/javascript" src="cw2.js"></script>
<script type="text/javascript" src="wz_jsgraphics.js"></script>
<script type="text/javascript" src="cw2_edit_map.js"></script>

</head>

<style>
div {margin:0}
</style>

<body id="body_id" onload="doOnLoad()" onSubmit="doSave();" style="margin:0; background-color:06283b">

<?php

function SaveInput($tournament_id)
{
 global $dbl, $planets, $tournament, $edit_mode;
 if (GetTournament($tournament_id)<0) {return -2;}
 if (GetPlanets($tournament_id)<0) {return -2;}
 
 if (IsSet($_POST['edit_mode']))
 {
  $edit_mode = IntVal($_POST['edit_mode']);
 }
 
 if (IsSet($_POST['map_img_url']))
 {
  $map_img_url = $_POST['map_img_url'];
  if ($map_img_url != $tournament['map_img_url'])
  {
   $sql = "UPDATE tournaments SET map_img_url='".mysql_real_escape_string($map_img_url, $dbl)."' WHERE id='$tournament_id'";
   if (!mysql_query($sql,$dbl))
   {
    fecho("Update map_img_url - query ", $sql, " failed : ", mysql_error());
    return -2;
   }
  }
 }
 
 $npl = 0;
 $updating = false;
 while (IsSet($_POST['planet_name_'.($npl+1)]))
 {
  $updating = true;
  $npl++;
  $planet_name = $_POST['planet_name_'.$npl];
  $idx = 'planet_data_'.$npl;
  if (!IsSet($_POST[$idx])) {echo "<br>Planet=".$npl." - not enough data (no post)"; return -2;}
  $planet_data_txt = $_POST[$idx];
  $data = explode('_',$planet_data_txt);
  $ndata = count($data);
  if ($ndata < 5) {echo "<br>Planet=".$npl." - not enough data ($ndata)"; return -2;}
  $planet_id = IntVal($data[2]);
  $planet_x = IntVal($data[0]);
  $planet_y = IntVal($data[1]);
  $planet_size = IntVal($data[3]);
  $map_id = IntVal($data[4]);
  $planet_starlanes = array();
  for ($i = 0; $i < CW2_MAX_STARLANES; $i++)
  {
   $planet_starlanes[$i] = IntVal($data[$i+5]);
  }
  
  if (($planet_id > 0) && (!IsSet($planets[$planet_id])))
  {
   $planet_id = -1;
  }
  
  if ($planet_id > 0)
  {
   $planets[$planet_id]['keep_it'] = 1;
   $sql = "UPDATE cw2_planets SET ";
   $sql_where = "WHERE planet_id='$planet_id';";
   $sql_action_txt = "Update planet $planet_id";
   if ($planet_name != $planets[$planet_id]['planet_name'])
   {
    $sql.= "planet_name = '" . mysql_real_escape_string($planet_name, $dbl) . "', ";
   }
  }
  else
  {
   $sql = "INSERT INTO cw2_planets SET tournament_id = '$tournament_id', ";
   $sql_where = ";";
   $sql_action_txt = "Insert new planet";
  }
  $sql.= "planet_x = '$planet_x', planet_y = '$planet_y', planet_size = '$planet_size', map_id = '$map_id'";
  foreach ($planet_starlanes as $i => $planet_starlane)
  {
   $sql.= ", starlane".$i." = '$planet_starlane' ";
  }
  $sql.= $sql_where;
  //echo "<br>SQL = ".htmlspecialchars($sql)."<br>";
  if (!mysql_query($sql,$dbl))
  {
   fecho($sql_action_txt." - query ", $sql, " failed : ", mysql_error());
   return -2;
  }
 }
 
 if ($updating)
 {
  $sql = "DELETE FROM cw2_planets WHERE planet_id IN(";
  $count_deleted = 0;
  foreach ($planets as $planet_id=>$planet)
  {
   if (!IsSet($planet['keep_it']))
   {
    if ($count_deleted)
    {
     $sql.= ", ";
    }
    $sql.="$planet_id";
    $count_deleted++;
   }
  }
  if ($count_deleted)
  {
   $sql.=");";
   if (!mysql_query($sql,$dbl))
   {
    fecho("Delete planets - query ", $sql, " failed : ", mysql_error());
    return -2;
   }
  }
 }
  
 unset($GLOBALS['tournament']);
 unset($GLOBALS['planets']);
 
 return 1;
}

function EditPlanets()
{
 global $dbl, $edit_mode;

 $dbl=DBConn();
 $tournament_id = 0;
 $edit_mode = 0;
 if (IsSet($_POST['tournament_id']))
 {
  $tournament_id = IntVal($_POST['tournament_id']);
 }
 else if (IsSet($_GET['tournament_id']))
 {
  $tournament_id = IntVal($_GET['tournament_id']);
 }
 if ($tournament_id == 0)
 {
  return -1;
 }
 
 if (GetMaps() < 0) {return -1;}
 
 if (SaveInput($tournament_id) < 0) {return -2;}
 
 global $tournament, $planets, $maps;
 
 if (GetTournament($tournament_id) < 0) {return -1;}
 if (GetPlanets($tournament_id) < 0) {return -1;}

 if (trim($tournament['map_img_url']==""))
 {
  $edit_mode = 2;
 }

 echo '
 <FORM ID="form_id" action="cw2_edit_map.php" method="post">
 <table style="color:white">
 <tr height="45"><td id="edit_mode_text" style="color:white">
 </td></tr>

 <tr><td>
 <INPUT TYPE=BUTTON OnClick="changeEditMode(mode_map_image);" VALUE="'.$GLOBALS['lang_cw2_edit_map_image'].'">&nbsp;&nbsp;&nbsp;
 <INPUT TYPE=BUTTON OnClick="changeEditMode(mode_planet_coordinates);" VALUE="'.$GLOBALS['lang_cw2_edit_planet_coordinates'].'">&nbsp;&nbsp;&nbsp;';
// <INPUT TYPE=BUTTON OnClick="changeEditMode(mode_planet_reflections);" VALUE="'.$GLOBALS['lang_cw2_edit_planet_reflections'].'">&nbsp;&nbsp;&nbsp;
echo '
 <INPUT TYPE=BUTTON OnClick="changeEditMode(mode_planet_stats);" VALUE="'.$GLOBALS['lang_cw2_edit_planet_stats'].'">&nbsp;&nbsp;&nbsp;
 <INPUT TYPE=BUTTON OnClick="changeEditMode(mode_starlanes);" VALUE="'.$GLOBALS['lang_cw2_edit_starlanes'].'">&nbsp;&nbsp;&nbsp;
 <INPUT TYPE=SUBMIT VALUE="'.$GLOBALS['lang_cw2_save__caps'].'">
 </td></tr>
 
 <tr height="24" id="tr_starlanes" style="display:none"><td>
 '.$GLOBALS['lang_cw2_show_all_starlanes'].':&nbsp;<INPUT TYPE=CHECKBOX id="show_all_starlanes" onchange="changeShowAllStarlanes();">&nbsp;&nbsp;&nbsp; <INPUT TYPE=BUTTON OnClick="deleteAllStarlanes();" VALUE="'.$GLOBALS['lang_cw2_delete_all_starlanes'].'">&nbsp;&nbsp;&nbsp;

 </td></tr>
 
 <tr height="24" id="tr_planet_stats" style="display:none"><td>
 '.$GLOBALS['lang_cw2_planet_name'].':&nbsp;<INPUT TYPE=TEXT id="planet_name" SIZE="30" onchange="changePlanetName();" VALUE="">&nbsp;&nbsp;&nbsp;
 '.$GLOBALS['lang_cw2_planet_size'].':&nbsp;<INPUT TYPE=TEXT id="planet_size" SIZE="5" onchange="changePlanetSize();" VALUE="">&nbsp;&nbsp;&nbsp;
 ';
 
 echo $GLOBALS['lang_cw2_planet_map'].':&nbsp;<SELECT id="planet_map" onchange="changePlanetMap();">
<option value="-1">---</option>';
 foreach ($maps as $map_id=>$map)
 {
  echo '
<option value="'.$map_id.'">'.$map['map_name_'.$GLOBALS['lang']].'</option>';
 }
 echo '
</SELECT>&nbsp;&nbsp;&nbsp;';

 echo '
 </td></tr>
 <tr height="24" id="tr_nothing"><td></td></tr>
 <tr height="24" id="tr_map_image" style="display:none"><td><INPUT TYPE=TEXT id="map_img_url" SIZE="50" onchange="changeMapImageURL();" VALUE='.$tournament['map_img_url'].'></td></tr>
 </table>
 </FORM>
 ';


 echo '<div id="map_div" style="position:relative;left:0px;top:0px;z-index:2;overflow:visible;">';

 echo '<img id="map_img" OnClick="gotClickXY(event);return true;" src="' . $tournament['map_img_url'] . '">';

 echo '</div>';
 echo '<div id="cw2log_div" style="color:white">&nbsp;</div>';

 $new_planet = -1;

 echo '

 
 <script type="text/javascript">
 var jg = new jsGraphics("map_div");
 
 var Planets = new Array();
 ';

 $count=0;
 foreach ($planets as &$planet)
 {
  $planet['js_id'] = $count;
  $count++;
 }
 
 foreach ($planets as &$planet)
 {
  echo '
  Planets['.$planet['js_id'].'] = new C_Planet('.$planet['planet_id'].', '.$planet['js_id'].', "'.$planet['planet_name'].'", '.$planet['planet_x'].', '.$planet['planet_y'].', '.$planet['planet_size'];
  echo ", new Array(";
  for ($j = 0; $j < CW2_MAX_STARLANES; $j++)
  {
   if ($j > 0) {echo ", ";}
   if ($planet['starlane'.$j] > 0)
   {
    echo $planets[$planet['starlane'.$j]]['js_id'];
   }
   else
   {
    echo CW2_NO_OBJECT;
   }
  }
  echo '), '.$planet['map_id'].');';
 }
 
 echo '

 var planet_size_texts = new Array(';
 foreach ($GLOBALS['lang_cw2_planet_size_short_texts'] as $i => $val)
 {
  if ($i > 0) {echo ', ';}
  echo '"'.$val.'"';
 }
 echo ');';
 
 $curr_planet = CW2_NO_OBJECT;

 echo "
 var edit_mode = $edit_mode;
 var curr_planet = $curr_planet;
 var CW2_NO_OBJECT = ".CW2_NO_OBJECT.";
 var max_starlanes = ".CW2_MAX_STARLANES.";
 var tournament_id = $tournament_id;
 var show_all_starlanes = 0;
 ";
 
 echo '
 var lang_cw2_editing_planet_coordinates = "' . jst($GLOBALS['lang_cw2_editing_planet_coordinates']) . '";
 var lang_cw2_editing_planet_information = "' . jst($GLOBALS['lang_cw2_editing_planet_information']) . '";
 var lang_cw2_editing_map_image = "' . jst($GLOBALS['lang_cw2_editing_map_image']) . '";
 var lang_cw2_editing_planet_reflections = "' . jst($GLOBALS['lang_cw2_editing_planet_reflections']) . '";
 var lang_cw2_editing_starlanes = "' . jst($GLOBALS['lang_cw2_editing_starlanes']) . '";
 var lang_cw2_tournament_not_selected = "' . jst($GLOBALS['lang_cw2_tournament_not_selected']) . '";
 ';

 echo '
 
 changeEditMode(edit_mode);

 for (i in Planets)
 {
  updatePlanetDiv(i);
 }

 </script>
 ';

}

//MAIN

//var_dump($_POST);


$result = EditPlanets();
if ($result < 0)
{
 $edit_mode = -1;
 echo "<script type=\"text/javascript\"> var edit_mode = $edit_mode; document.getElementById(\"body_id\").style.backgroundColor=\"white\";var planets = new Array();</script>";
 echo '$_POST=<br>';
 var_dump($_POST);
}
 


?>

</body>
</html>
